package com.hxgz.steel.controller.portal;

import com.hxgz.steel.common.Const;
import com.hxgz.steel.common.ResponseCode;
import com.hxgz.steel.common.ServerResponse;
import com.hxgz.steel.domain.Merchant;
import com.hxgz.steel.domain.User;
import com.hxgz.steel.dto.MerchantDto;
import com.hxgz.steel.service.MerchantService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 我的店铺
 */
@RestController
@RequestMapping("merchant")
@Api(tags = "MerchantController",description = "前台-店铺相关")
public class MerchantController {

    @Autowired
    MerchantService mService;

    /**
     * 新增或编辑店铺信息
     * @param merchantDto 店铺对象
     * @return
     */
    @RequestMapping(value = "insertOrUpdateMerchant.do",method = RequestMethod.POST)
    @ApiOperation("卖家中心-新增/编辑店铺信息")
    public ServerResponse insertOrUpdateMerchant(MerchantDto merchantDto, HttpServletRequest httpServletRequest){
        User user = Const.checkUserLogin(httpServletRequest);
        if(user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        String[] str = merchantDto.getBusinessScope().split("-");
        for (int i = 0;i<str.length;i++){
            if (!str[i].equals("1") && !str[i].equals("2") && !str[i].equals("3") && !str[i].equals("4")){
                return ServerResponse.createByErrorMessage("经营范围异常");
            }
        }
        Merchant merchant = new Merchant();
        merchant.setUserId(user.getId());
        merchant.setId(merchantDto.getId());
        merchant.setLogo(merchantDto.getLogo());
        merchant.setName(merchantDto.getName());
        merchant.setAddress(merchantDto.getAddress());
        merchant.setAddressInfo(merchantDto.getAddressInfo());
        merchant.setBusinessScope(merchantDto.getBusinessScope());
        merchant.setProcess(merchantDto.getProcess());
        merchant.setSpotGoods(merchantDto.getSpotGoods());
        merchant.setVideoInterface(merchantDto.getVideoInterface());

        return mService.insertOrUpdateMerchant(merchant);
    }


    /**
     * 查询商家的店铺信息
     * @return
     */
    @RequestMapping(value = "getMerchant.do",method = RequestMethod.POST)
    @ApiOperation("卖家中心-查询信息")
    public ServerResponse<Merchant> getMerchantByuserId(HttpServletRequest httpServletRequest){
        User user = Const.checkUserLogin(httpServletRequest);
        if(user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return mService.getMerchantByuserId(user.getId());
    }


   /* @RequestMapping(value = "hotMerchant.do",method = RequestMethod.POST)
    @ApiOperation("首页-人气店铺")
    public ServerResponse<PageInfo<List<HotStore>>> hotMerchant(HttpServletRequest httpServletRequest, Page page){
        User user = Const.checkUserLogin(httpServletRequest);
        if(user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return mService.hotMerchant(page);
    }*/
}
